//
// Created by xiaoma on 1/28/21.
//

#include "ArrayList.h"

template<typename T>
ArrayList<T>::ArrayList(int n) {
    _len = n;
    _size = 0;
    _array = new T[n];
}

template<typename T>
void ArrayList<T>::add(T t) {
    if (_size >= _len) {
        expand();
    }
    _array[_size++] = t;
}

template<typename T>
void ArrayList<T>::insert(int index, T t) {
    if (_size >= _len) {
        expand();
    }
    for (int i = _size; i >= index; --i) {
        _array[i + 1] = _array[i];
    }
    _array[index] = t;
}

template<typename T>
void ArrayList<T>::expand() {
    T *tmp = _array;
    _array = new T[_len << 1];
    for (int i = 0; i < _len; ++i) {
        _array[i] = tmp[i];
    }
    _len <<= 1;
    delete[] tmp;
}

class HiObject;

template
class ArrayList<HiObject *>;

class HiString;

template
class ArrayList<HiString *>;